--[[
Project: HunterRP
File: cl_drawfuncs.lua
Author: Sebihunter
]]--

--Parts of this file taken from "DarkRP"

rp_hud={}
stamina={}

require("datastream")

economy = 1
broadcasttext = ""
isbroadcast = 0

local colors =
{
	background =
	{
		border = Color(0, 0, 0, 255),
		background = Color(0, 0, 0, 75)
	},
	
	text=
	{
		shadow = Color(0, 0, 0, 200),
		text = Color(255, 255, 255, 255)
	},
	
	health_bar =
	{
		border = Color(0, 0, 0, 255),
		background = Color(255, 0, 0, 75),
		shade = Color(255, 104, 104, 255),
		fill = Color(232, 0, 0, 255)
	},
	
	stamina_bar =
	{
		border = Color(0, 0, 0, 255),
		background = Color(245, 135, 25, 75),
		shade = Color(245, 170, 95, 255),
		fill = Color(245, 135, 25, 255)
	},
	
	ecob_bar =
	{
		border = Color(0, 0, 0, 255),
		background = Color(255, 0, 0, 75),
		shade = Color(255, 104, 104, 255),
		fill = Color(200, 10, 0, 255)
	},	
	
		ecog_bar =
	{
		border = Color(0, 0, 0, 255),
		background = Color(0, 255, 0, 75),
		shade = Color(104, 255, 104, 255),
		fill = Color(10, 200, 0, 255)
	}
};

local function clr(color)
	return color.r, color.g, color.b, color.a;
end

function rp_hud:PaintBar(x, y, w, h, colors, value)

	self:PaintPanel(x, y, w, h, colors);
	
	x = x + 1;
	y = y + 1;
	w = w - 2;
	h = h - 2;
	
	local width = w * math.Clamp(value, 0, 1);
	local shade = 4;
	
	surface.SetDrawColor(clr(colors.shade));
	surface.DrawRect(x, y, width, shade);
	
	surface.SetDrawColor(clr(colors.fill));
	surface.DrawRect(x, y + shade, width, h-shade);
	
end 

function rp_hud:PaintBarBackwards(x, y, w, h, colors, value)

	self:PaintPanel(x, y, w, h, colors);
	
	x = x + 1;
	y = y + 1;
	w = w - 2;
	h = h - 2;

	local width = w * math.Clamp(value, 0, 1);
	local shade = 4;
	
	surface.SetDrawColor(clr(colors.shade));
	surface.DrawRect((x+w)-width, y, width, shade);
	
	surface.SetDrawColor(clr(colors.fill));
	surface.DrawRect((x+w)-width, y + shade, width, h-shade);
	
end 

function rp_hud:PaintPanel(x, y, w, h, colors)
	surface.SetDrawColor(clr(colors.border));
	surface.DrawOutlinedRect(x, y, w, h);
	
	x = x + 1;
	y = y + 1;
	w = w - 2;
	h = h - 2;
	
	surface.SetDrawColor(clr(colors.background));
	surface.DrawRect(x, y, w, h);

end

function showPlayerMessagebox( handle, id, encoded, decoded )
	local selectionframe = vgui.Create("DFrame")
	selectionframe:Center()
	selectionframe:SetSize(200,100)
	selectionframe:SetTitle(decoded.title)
	selectionframe:MakePopup()
	selectionframe:SetDraggable( false ) 
	selectionframe:ShowCloseButton( false ) 
	
	Label1 = vgui.Create("Label", selectionframe)
	Label1:SetPos(3,25)
	Label1:SetText(decoded.text)
	Label1:SizeToContents()
	
	
	local button = vgui.Create("Button", selectionframe)
	button:SetPos( 75, 75 );
	button:SetSize( 50,20 )
	button:SetText( "Ok" )
	
	
	selectionframe:Center()

	button.DoClick = function()
		surface.PlaySound( "ui/buttonclickrelease.wav" )
		selectionframe:SetVisible(false)
	end
end

datastream.Hook( "showPlayerMessagebox", showPlayerMessagebox );

function showPlayerSelectionMessagebox( handle, id, encoded, decoded )
	local selectionframe = vgui.Create("DFrame")
	selectionframe:Center()
	selectionframe:SetSize(200,100)
	selectionframe:SetTitle(decoded.title)
	selectionframe:MakePopup()
	selectionframe:SetDraggable( false ) 
	selectionframe:ShowCloseButton( false ) 
	
	Label1 = vgui.Create("Label", selectionframe)
	Label1:SetPos(3,25)
	Label1:SetText(decoded.text)
	Label1:SizeToContents()
	
	
	local button = vgui.Create("Button", selectionframe)
	button:SetPos( 15, 75 );
	button:SetSize( 50,20 )
	button:SetText( decoded.button1 )
	
	local button2 = vgui.Create("Button", selectionframe)
	button2:SetPos( 135, 75 );
	button2:SetSize( 50,20 )
	button2:SetText( decoded.button2 )	
	
	
	selectionframe:Center()
	
	button.DoClick = function()
		surface.PlaySound( "ui/buttonclickrelease.wav" )
		datastream.StreamToServer( decoded.callfunc );
		selectionframe:SetVisible(false)
	end
	
	button2.DoClick = function()
		surface.PlaySound( "ui/buttonclickrelease.wav" )
		selectionframe:SetVisible(false)
	end
end

datastream.Hook( "showPlayerSelectionMessagebox", showPlayerSelectionMessagebox );

function playerHUD()
	staminaFunc()
	local client = LocalPlayer()
	if !client:Alive() then return end
	if(client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end

	local mag_left = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
	local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
	local secondary_ammo = client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType())// How much ammunition you have for your secondary fire, such as the MP7's grenade launcher
	
	draw.RoundedBox(0, 3, ScrH()-100, 200, 95, Color(0, 0, 0, 150))
	if client:GetNWString("name") ~= "" and client:GetNWString("name") ~= "Minge Bag" then
		draw.SimpleTextOutlined(client:GetNWString("name"), "ScoreboardText", 5, ScrH()-95, Color(255, 165, 0, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
	else
		draw.SimpleTextOutlined(client:Nick(), "ScoreboardText", 5, ScrH()-95, Color(255, 165, 0, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
	end
	draw.SimpleText(team.GetName(client:Team()), "BudgetLabel", 5, ScrH()-80, Color(125, 125, 125, 255), 0, 0)

	draw.SimpleTextOutlined("$", "Trebuchet20", 5, ScrH()-60, Color(0, 165, 0, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
	draw.SimpleTextOutlined(tostring(client:GetNWInt("money")), "Trebuchet20", 20, ScrH()-60, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))	
	
	--draw.SimpleTextOutlined("Health:", "Trebuchet20", 5, ScrH()-40, Color(165, 0, 0, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
	--draw.SimpleTextOutlined(client:Health() .. "%", "Trebuchet20", 60, ScrH()-40, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
	rp_hud:PaintBar(5, ScrH()-35, 195, 10, colors.health_bar, client:Health() /100);	
	rp_hud:PaintBar(5, ScrH()-24, 195, 10, colors.stamina_bar, (stamina[LocalPlayer()]) /1000);
	
	if economy > 1 then
		rp_hud:PaintBarBackwards(ScrW()/2-100, 30, 100, 10, colors.ecob_bar, 0);
		rp_hud:PaintBar(ScrW()/2, 30, 100, 10, colors.ecog_bar, economy-1);
	elseif economy < 1 then
		rp_hud:PaintBarBackwards(ScrW()/2-100, 30, 100, 10, colors.ecob_bar, 1-economy);
		rp_hud:PaintBar(ScrW()/2, 30, 100, 10, colors.ecog_bar, 0);
	elseif economy == 0 then
		rp_hud:PaintBarBackwards(ScrW()/2-100, 30, 100, 10, colors.ecob_bar, 1);
		rp_hud:PaintBar(ScrW()/2, 30, 100, 10, colors.ecog_bar, 0);	
	else
		rp_hud:PaintBarBackwards(ScrW()/2-100, 30, 100, 10, colors.ecob_bar, 0);
		rp_hud:PaintBar(ScrW()/2, 30, 100, 10, colors.ecog_bar, 0);	
	end
	
	draw.SimpleTextOutlined("Economy", "Trebuchet14", ScrW()/2, 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
	
	if broadcasttext ~= "" then
		local QuadTable = {} 
 
		QuadTable.texture 	= surface.GetTextureID( "gui/gradient_up" )
		QuadTable.color		= Color( 10, 10, 10, 125 ) 
 
		QuadTable.x = ScrW()/2-150 
		QuadTable.y = 65
		QuadTable.w = 300 
		QuadTable.h = 3
		draw.TexturedQuad( QuadTable )
		
		QuadTable.texture 	= surface.GetTextureID( "gui/gradient_down" )
		QuadTable.y = 87
		draw.TexturedQuad( QuadTable )
		
		draw.SimpleTextOutlined(broadcasttext, "Trebuchet14", ScrW()/2, 77, Color(125, 125, 125, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
	end
	
	local magdis = 0
	if mag_left == 0 then	
		magdis = 1
	end
	
	if mag_left ~= -1 then
		if magdis ~= 1 then
			draw.SimpleTextOutlined(mag_left .. " | " .. mag_extra, "HUDNumber", ScrW()-100, ScrH()-40, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
		end	
	end	
	
	draw.SimpleTextOutlined("HunterRP developed by Sebihunter -  v0.1", "CloseCaption_Normal", ScrW()-10, 5, Color(255, 255, 255, 255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
	draw.SimpleTextOutlined("www.sebihunter.de", "CloseCaption_Normal", ScrW()-10, 20, Color(255, 255, 255, 255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
	
	local trace = LocalPlayer():GetEyeTrace()
	if ValidEntity(trace.Entity) and trace.Entity:GetPos():Distance(LocalPlayer():GetPos()) < 400 then
		if trace.Entity:IsDoor() then
			local pos = trace.Entity:GetPos():ToScreen()
			
			if tonumber(trace.Entity:GetNWInt("ownable")) == 0 then
				draw.SimpleTextOutlined(trace.Entity:GetNWString ("name"), "Trebuchet20", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
			else
				if tostring(trace.Entity:GetNWString ("owner")) == "nobody" then
					draw.SimpleTextOutlined(trace.Entity:GetNWString ("name"), "Trebuchet20", pos.x, pos.y-15, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
					draw.SimpleTextOutlined("Price: "..trace.Entity:GetNWInt ("price").."$", "Trebuchet20", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
				else
					draw.SimpleTextOutlined(trace.Entity:GetNWString ("name"), "Trebuchet20", pos.x, pos.y-15, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
					draw.SimpleTextOutlined("Owner: "..trace.Entity:GetNWString ("owner"), "Trebuchet20", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0,0,0,255))
				end				
			end				
			
		end
		
		
		if trace.Entity:GetClass() == "npc_eli" then
				local pos = trace.Entity:EyePos()
				pos.z = pos.z + 14
				pos = pos:ToScreen()
				if trace.Entity:GetNWInt("npcid") == 1 then
					draw.SimpleTextOutlined("Police Recruitment", "Trebuchet20", pos.x, pos.y-15, Color(0, 0, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
					draw.SimpleTextOutlined("become a police officer", "Trebuchet20", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
				elseif trace.Entity:GetNWInt("npcid") == 2 then
					draw.SimpleTextOutlined("Ambulance Recruitment", "Trebuchet20", pos.x, pos.y-15, Color(125, 0, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
					draw.SimpleTextOutlined("become a medic", "Trebuchet20", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
				elseif trace.Entity:GetNWInt("npcid") == 3 then
					draw.SimpleTextOutlined("Goverment Reception", "Trebuchet20", pos.x, pos.y-15, Color(255, 165, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
					draw.SimpleTextOutlined("sign in for the mayor vote", "Trebuchet20", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))					
				else
					draw.SimpleTextOutlined("Generic NPC", "Trebuchet20", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
				end	
		end
	end
	
	
	--[[if trace.Entity:IsPlayer() then 
		local pos = client:EyePos()
		pos.z = pos.z + 14
		pos = pos:ToScreen()
		
		draw.DrawText(trace.Entity:Nick(), "TargetID", pos.x + 1, pos.y + 1, Color(0, 0, 0, 255), 1)
		draw.DrawText(trace.Entity:Nick(), "TargetID", pos.x, pos.y, team.GetColor(trace.Entity:Team()), 1)
	end]]--
end
hook.Add("HUDPaint", "playerHUD", playerHUD)

function hideHUD(name)
	--CHudWeaponSelection disablen - Neuen weapon selector einbauen!
	for k, v in pairs{"CHudHealth", "CHudBattery", "CHudSuit", "CHudAmmo", "CHudSecondaryAmmo", "CHudCrosshair"} do
		if name == v then return false end
	end
end
hook.Add("HUDShouldDraw", "hideHUD", hideHUD)

function GM:HUDDrawScoreboard()
return false; // Return true to make it allowed.
end

function startBroadcast(text)
	if isbroadcast == 1 then
		timer.Destroy("broadcastTimer")
	end	
	isbroadcast = 1
	timer.Create( "broadcastTimer", 10, 1, function()
		isbroadcast = 0
		broadcasttext = ""
	end )
	broadcasttext = text
end

function setClientCityEconomy( handle, id, encoded, decoded )
	if (economy > decoded.economy) then
		startBroadcast("Eco: "..tostring((decoded.economy-economy)*100))
	elseif (economy < decoded.economy) then
		startBroadcast("Eco: +"..tostring((decoded.economy-economy)*100))
	else
		startBroadcast("Eco: "..tostring(decoded.economy*100))
	end
	economy = decoded.economy
end
datastream.Hook( "setClientCityEconomy", setClientCityEconomy );
 